package com.check.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.check.dao.PersonRoleDao;
import com.check.dao.ResourceDao;
import com.check.dao.RoleDao;
import com.check.pojo.PageObject;
import com.check.pojo.Resource;
import com.check.pojo.RoResource;
import com.check.pojo.Role;
import com.check.service.RoleService;

@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	@Qualifier("roleDao")
	private RoleDao roleDao;
	
	@Autowired
	@Qualifier("reDao")
	private ResourceDao reDao;
	
	@Autowired
	@Qualifier("personRoleDao")
	private PersonRoleDao personRoleDao;
	
	public List<Role> findAllRole(PageObject pageObject) {
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("startNum", pageObject.getStartNum());
		map.put("endNum", pageObject.getEndNum());
		return roleDao.findRoleList(map);
	}
	
	public int findRoleCount(){
		return roleDao.findRoleCount();
	}
	
	public boolean addRole(Role role) {
		boolean flag = false;
			try{
				roleDao.addRole(role);
				flag = true;
			}catch (Exception e) {
				e.printStackTrace();
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() ;
				flag = false;
			}
		return flag;
	}

	public boolean deleteRole(String roId) {
		boolean flag = false;
		try{
			roleDao.removeRole(roId);
			flag = true;
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() ;
			flag = false;
		}
		return flag;
	}

	public Role findRoleById(String roId) {
		Role role = null;
		
		try {
			role = roleDao.findRoleById(roId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return role;
	}

	public boolean updateRole(Role role) {
		boolean flag = false;
		try{
			roleDao.updateRole(role);
			flag = true;
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() ;
			flag = false;
		}
	return flag;
		
	}

	public String getTreeJson(String roId) {
		List<Resource> reList  = roleDao.findReList(roId);
		Map<Integer,Resource> tempMap = new LinkedHashMap<Integer,Resource>();
		StringBuffer buf = new StringBuffer("[");
		int temp =0; 
		for(Resource re : reList){
			if(re.getrFId() == temp){
				buf.append("{");
				
				buf.append("id:"+re.getrId()+",");
				buf.append("pId:"+re.getrFId()+",");
				buf.append("name:'"+re.getrName()+"'");
				if(re.getIsChecked() == 0){
					buf.append(",checked:true");
					Resource reTmp = re;
					tempMap.put(reTmp.getrFId(),reTmp);
				}
				
				buf.append("},");
				
			}else{
				
				buf.append("{");
				
				buf.append("id:"+re.getrId()+",");
				buf.append("pId:"+re.getrFId()+",");
				buf.append("name:'"+re.getrName()+"'");
				if(re.getIsChecked() == 0){
					buf.append(",checked:true");
				}
				buf.append("},");
				Resource reTmp = re;
				tempMap.put(reTmp.getrFId(),reTmp);
				temp = re.getrFId();
			}
		}
		
		for (Map.Entry<Integer, Resource> entry : tempMap.entrySet()) {  
			
			buf.append("{");
			
			buf.append("id:"+ entry.getKey() +",");
			buf.append("pId:"+ 0 +",");
			buf.append("name:'"+ entry.getValue().getrFName() +"',");
			buf.append("open:true");
			if(entry.getValue().getIsChecked() == 0){
				buf.append(",checked:true");
			}
			buf.append("},");
        }  
		
		buf.append("]");
//		System.out.println(buf.toString());
		return buf.toString();
	}

	public boolean addRoRe(RoResource roRe) {
		boolean flag = false;
		
		try{
			reDao.addRoRe(roRe);
			flag = true;
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public boolean deleteRoRe(String roId) {
		boolean flag = false;
		
		try{
			reDao.removeRoRe(roId);
			personRoleDao.deletePersonOfRole(roId);
			flag = true;
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() ;
			flag=false;
		}
		return flag;
	}

	public List<Role> findAllRole() {
		return roleDao.findAllRole();
	}

}
